﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DBUtility;
using CUREEntity;
using System.Data;
using CUREDataAccessLayers.Common;
namespace CUREDataAccessLayers
{
    public class MenuDal
    {
        private common comm = new common();

        /// <summary>
        /// 菜单信息分页方法
        /// </summary>
        /// <param name="where">条件</param>
        /// <param name="pageIndex">当前页索引</param>
        /// <param name="pageSize">每页显示的数据条数</param>
        /// <param name="totalRecordCount">总记录数</param>
        /// <param name="totalPageCount">总页数</param>
        /// <returns></returns>
        public DataSet GetMenuData(IDictionary<string, object> where, int pageIndex, int pageSize, out int totalRecordCount, out int totalPageCount)
        {
            //IList<MenuEntity> list = null;
            DataSet ds = new DataSet();
            StringBuilder commandText = new StringBuilder();
            //commandText.Append("select t.action_columnid as MENU_ID,");
            //commandText.Append("t.action_columnname as MENU_NAME");
            //commandText.Append(" from actioncolumn t");
            //commandText.Append(" where 1=1");
            if (where["name"] != null && where["name"].ToString() != "")
            {
                commandText.Append(" and t.action_columnname like '%"+where["name"].ToString().Trim()+"%'");
            }
            try
            {
                ds = comm.GetList("actioncolumn", pageSize, pageIndex, commandText.ToString(), "action_colsort asc", out totalRecordCount, out totalPageCount);
               // list = this.DbHelperInstance.GetPagedData<MenuEntity>(pageIndex, pageSize, commandText, " MENU_ID asc ", out totalRecordCount, out totalPageCount);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return ds;
        }
        /// <summary>
        ///添加菜单信息
        /// </summary>
        /// <param name="id">菜单id</param>
        /// <param name="menuName">菜单名</param>
        /// <returns></returns>
        public int InsertMenu(int id, string menuName,string menuSort)
        {
            int result = 0;
            StringBuilder commandText = new StringBuilder();
            try
            {
                commandText.Append("insert into actioncolumn(action_columnid,action_columnname,action_colsort)values('" + id + "','" + menuName.Trim() + "','" + menuSort + "')");
                result = DbHelperOra.ExecuteSql(commandText.ToString());
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }
        /// <summary>
        /// 更新菜单信息
        /// </summary>
        /// <param name="id">id</param>
        /// <param name="menuName">菜单名称</param>
        /// <returns></returns>
        public int UpdateMenu(string id, string menuName,string menuSort)
        {
            int result = 0;
            StringBuilder commandText = new StringBuilder();
            try
            {
                if (!string.IsNullOrEmpty(id) && !string.IsNullOrEmpty(menuName))
                {
                    commandText.Append("update actioncolumn set action_columnname='" + menuName.Trim() + "',action_colsort='" + menuSort + "' where action_columnid='" + id.Trim() + "'");
                    result = DbHelperOra.ExecuteSql(commandText.ToString());
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }
        /// <summary>
        /// 删除菜单信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int DeleteMenu(string id)
        {
            int result = 0;
            StringBuilder commandText = new StringBuilder();
            try
            {
                if (!string.IsNullOrEmpty(id))
                {
                    commandText.Append("delete from actioncolumn where action_columnid='" + id.Trim() + "'");
                    result = DbHelperOra.ExecuteSql(commandText.ToString());
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }
        /// <summary>
        /// 获取最大的id
        /// </summary>
        /// <returns></returns>
        public decimal GetMaxId()
        {
            decimal maxid = 0;
            StringBuilder commandText = new StringBuilder();
            try
            {
                commandText.Append("select max(action_columnid) from actioncolumn");
                maxid = DbHelperOra.GetMaxID("action_columnid", "actioncolumn");
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return maxid;
        }
        /// <summary>
        /// 根据id获取菜单信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public DataTable GetMenuInfoById(string id)
        {
            DataTable dt = new DataTable();
            StringBuilder commandText = new StringBuilder();
            try
            {
                commandText.Append("select * from actioncolumn where action_columnid='"+id+"'");
                dt = DbHelperOra.QueryTable(commandText.ToString());
                //this.DbHelperInstance.Fill(dt, commandText);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }
        /// <summary>
        /// 根据菜单名称获取信息
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public DataTable GetMenuByName(string name)
        {
            DataTable dt = new DataTable();
            StringBuilder commandText = new StringBuilder();
            try
            {
                commandText.Append("select * from actioncolumn where action_columnname='"+name.Trim()+"'");
                dt = DbHelperOra.QueryTable(commandText.ToString());
               // this.DbHelperInstance.Fill(dt, commandText);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }
        /// <summary>
        /// 根据id和名称获取数据
        /// </summary>
        /// <param name="id"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public DataTable GetMenuByIdAndName(string id, string name)
        {
            DataTable dt = new DataTable();
            StringBuilder commandText = new StringBuilder();
            try
            {
                commandText.Append("select * from actioncolumn where action_columnid!='" + id + "' and action_columnname='"+name+"' ");
                dt = DbHelperOra.QueryTable(commandText.ToString());
               // this.DbHelperInstance.Fill(dt,commandText);
            }
            catch(Exception ex)
            {
                throw ex;
            }
            return dt;
        }
    }
}
